package com.example.baselibs.base.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;

public abstract class BaseVLayoutAdapter extends DelegateAdapter.Adapter<BaseVLayoutAdapter.BaseViewHolder> implements IAdapter{


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return getLayoutHelper();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BaseViewHolder(inflateView(parent,getLayoutId(),addRootView()));
    }
    private View inflateView(ViewGroup parent,int layout,boolean haveViewGroup){
        View view=null;
        if (haveViewGroup){
            view= LayoutInflater.from(setContext()).inflate(layout,parent,false);
        }else {
            view=LayoutInflater.from(setContext()).inflate(layout,null,false);
        }
        return view;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        bindView(holder.itemView,position);
    }

    abstract LayoutHelper getLayoutHelper();

    @Override
    public int getItemCount() {
        return getCount();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder{
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

}
